#coding=utf-8
from models import *

from django.utils import simplejson  


import time,datetime

def ChangeModifiedTime():
    
    return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
# 获取所有规则
# 成功返回规则，保持数据库格式
def getAll():
    try:
        rule = UGRSDESCModel.objects
        if rule.count():
            return rule
        else:
            return False
    except:
        return False



# 获取一条规则
# 返回规则，保持数据库结构
def getOne(rid):
    rule = UGRSDESCModel.objects(id=rid)
    if rule.count():
        return rule[0]
    else:
        return False
    
    

# 从json添加一条规则
# 返回规则，保持数据库结构
def addOne(req):
    try:
        name = req['RSName']
#         print name + "\n"
        description = req['RSDescription']
#         print description + "\n"
        tag = req['RSTag']
#         print tag + "\n"
        salience = req['RSSalience']
#         print salience + "\n"
        status = str(req['RSStatus'])
#         print status + "\n"
        rhs = req['RSRHS']
        lhs = req['RSLHS']
        thislist = []
        thatlist = []
        if lhs:
            for l in lhs:
                this = LHSDESCModel(ASName=l['ASName'])
#                 print l['ASName']
                this.LHSType = l['LHSType']
#                 print l['LHSType']
                this.LHGCopmare = l['LHSCompare']
#                 print l['LHSCompare']
                this.LHSValue = l['LHSValue']
#                 print l['LHSValue']
                thislist.append(this)
        if rhs:
            for r in rhs:
                that = RHSDESCModel(ASName=r['ASName'])
#                 print r['ASName']
                that.RHSParam = r['RHSParam']
#                 print r['RHSParam']
                thatlist.append(that)
        
        rule = UGRSDESCModel(RSName=name)
        rule.RSModifiedtime = ChangeModifiedTime()
        rule.RSDescription = description
        rule.RSTag = tag
        rule.RSStatus = status
        rule.RSSalience = salience
        rule.LHSList = thislist
        rule.RHSList = thatlist
        rule.save()
        return rule;
    except:
        return False


# 根据规则id删除一条规则   
# 删除成功返回 True，不成功返回False
def deleteOne(rid):
    try:
        rule = UGRSDESCModel.objects(id=rid)
        if rule.count():
            rule.delete()
            return True
        else:
            return False
    except:
        return False


#更新一条规则，根据规则完整的json格式
# 更新成功返回True，失败返回False
def updateOne(req):
    try:
        rid = req['_id']['oid']
        rule = getOne(rid);
        name = req['RSName']
        description = req['RSDescription']
        tag = req['RSTag']
        salience = req['RSSalience']
        status = req['RSStatus']
        rhs = req['RSRHS']
        lhs = req['RSLHS']
        thislist = []
        thatlist = []
        if lhs:
            for l in lhs:
                this = LHSDESCModel(ASName=l['DSName'])
                this.LHSType = l['LHSType']
                this.LHGCopmare = l['LHGCompare']
                this.LHSValue = l['LHSValue']
                thislist.append(this)
        if rhs:
            for r in rhs:
                that = RHSDESCModel(ASName=r['RSName'])
                that.RHSParam = r['RHSParam']
                thatlist.append(that)
        rule.RSName = name        
        rule.RSModifiedtime = ChangeModifiedTime()
        rule.RSDescription = description
        rule.RSTag = tag
        rule.RSStatus = status
        rule.RSSalience = salience
        rule.LHSList = thislist
        rule.RHSList = thatlist
        rule.save()
        return rule;
                
    except:
        return False
    
# 修改当前规则运行状态
# 修改成功返回True，修改失败返回False    
def changeStatus(req):
    try:
        rid = req[id]
        status = req['RSStatus']
        rule = getOne(rid)
        rule.RSStatus = status
        rule.RSModifiedtime = ChangeModifiedTime()
        rule.save()
        return True
    except:
        return False



